package com.atrium.master.servicios;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import javax.inject.Inject;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.atrium.master.dao.GestorLibrosDao;
import com.atrium.master.modelo.Libro;

@ContextConfiguration("classpath:spring/test-context-hsql.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class GestorLibrosTestHSQL {
	
	private EmbeddedDatabase db;
	
	@Inject
	private GestorLibrosDao gestorLibrosDao;
	
	@Before
	public void setUp(){
		db = new EmbeddedDatabaseBuilder()
			.addScript("classpath:sql/embedded/schema.sql")
			.addScript("classpath:sql/embedded/test-data.sql").build();	
	}
	@Test
	public void comprobar_conexion_base_datos_hsql() throws Exception {
		JdbcTemplate template = new JdbcTemplate(db);
		int rowNums = template.queryForInt("select count(0) from libros");
		
		assertEquals(3, rowNums);
	}
	@Test
	public void dame_listado_libros_no_nulo(){
		List<Libro> listadoLibros = gestorLibrosDao.obtenerListadoLibros();
		
		assertNotNull(listadoLibros);
	}
	@After
	public void tearDown(){
		db.shutdown();
	}
}
